name: CI

env:
  Revision: ${{ github.run_number }}
  GITHUB_REF: "${{ github.ref }}"
  NSIS_ROOT: "C:/Program Files (x86)/NSIS"
  WINDDK: "C:/Program Files (x86)/Windows Kits/8.1"
  RELEASE_DIR: artifacts
  WINDOWS_TARGET: x86_64-pc-windows-msvc
  RELEASE_BIN: hc
  PROJECT_NAME: hc

on:
  push:
    branches: [ master ]
    tags:
      - "v*" # Run when tag matches v*, i.e. v1.0, v20.15.10
  pull_request:
    branches: [ master ]

  workflow_dispatch:

jobs:
  prepare:
    name: Preparation
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.get_version.outputs.VERSION }}
    steps:
      - name: Query version number
        id: get_version
        shell: bash
        run: |
          echo "using version tag ${GITHUB_REF:11}"
          echo ::set-output name=version::"${GITHUB_REF:11}"

  build:
    name: Building
    needs: [ prepare ]
    runs-on: [self-hosted, windows, x64]

    steps:
      - uses: actions/checkout@v2

      - name: Copy External
        shell: cmd
        run: |
          mkdir External
          xcopy d:\code\linq2hash\External External /S /Y
        
      - name: Add msbuild to PATH
        uses: microsoft/setup-msbuild@v1.1
          
      - run: nuget restore src\linq2hash.sln
      - name: Build
        working-directory: src
        shell: cmd
        env:
          PROJECT_BASE_PATH: "${{runner.workspace}}\\${{ env.PROJECT_NAME }}\\src"
          BINPLACE_PLACEFILE: "${{runner.workspace}}\\${{ env.PROJECT_NAME }}\\binplace.txt"
          HC_TEST_DIR: "${{runner.workspace}}\\${{ env.PROJECT_NAME }}\\_tst.net"
          RUNNER_CONTEXT: ${{ toJSON(runner) }}
          PROGRAM_VERSION: ${{ needs.prepare.outputs.version }}
        run: |
          echo "$RUNNER_CONTEXT"
          msbuild hc.xml

      - name: Packaging
        if: startsWith(github.ref, 'refs/tags/v')
        env:
          PROJECT_BASE_PATH: "${{runner.workspace}}\\${{ env.PROJECT_NAME }}\\src"
          PKG_PATH: pkg
        run: |
          mkdir ${{ env.RELEASE_DIR }}
          xcopy ${{ env.PROJECT_BASE_PATH }}\Install\Release ${{ env.RELEASE_DIR }}\* /S /Y
          mkdir ${{ env.PROJECT_BASE_PATH }}\${{ env.PKG_PATH }}
          xcopy ${{ env.PROJECT_BASE_PATH }}\x64\Release\ru ${{ env.PROJECT_BASE_PATH }}\${{ env.PKG_PATH }}\ru\* /S /Y
          xcopy ${{ env.PROJECT_BASE_PATH }}\Binplace-x64\Release\${{ env.RELEASE_BIN }}.exe ${{ env.PROJECT_BASE_PATH }}\${{ env.PKG_PATH }} /S /Y
          tar -cvzf .\${{ env.RELEASE_DIR }}\${{ env.RELEASE_BIN }}-${{ needs.prepare.outputs.version }}-${{ env.WINDOWS_TARGET }}.tar.gz -C ${{ env.PROJECT_BASE_PATH }}\${{ env.PKG_PATH }} *

      - uses: actions/upload-artifact@v2
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          name: windows
          path: .\${{ env.RELEASE_DIR }}

  release:
    name: GitHub Release
    needs: [ build ]
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - name: Query version number
        id: get_version
        shell: bash
        run: |
          echo "using version tag ${GITHUB_REF:11}"
          echo ::set-output name=version::"${GITHUB_REF:11}"

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.get_version.outputs.VERSION }}
          release_name: ${{ steps.get_version.outputs.VERSION }}

      - name: Download Windows zip
        uses: actions/download-artifact@v1
        with:
          name: windows

      - name: Create fake Cargo.toml
        run: |
          cat >Cargo.toml <<EOL
          [package]
          name = "${{ env.RELEASE_BIN }}"
          version = "${{ steps.get_version.outputs.VERSION }}"
          description = "Hash Calculator is the console tool that can calculate about 50 cryptographic hashes of strings and files."
          authors = ["egoroff <egoroff@gmail.com>"]
          homepage = "https://github.com/aegoroff/hc"
          license = "LGPL-3.0"
          EOL

      - name: Install Rust
        run: |
          rustup update stable --no-self-update
          rustup default stable
          cargo install releaser

      - name: Create scoop json
        run: /home/runner/.cargo/bin/releaser s -c . -b https://github.com/${{ github.repository }}/releases/download/${{ steps.get_version.outputs.VERSION }}/ -i './windows/' -e ${{ env.RELEASE_BIN }}.exe -u ${{ env.RELEASE_BIN }}.json
      - name: Publish scoop json
        uses: dmnemec/copy_file_to_another_repo_action@main
        env:
          API_TOKEN_GITHUB: ${{ secrets.GH_TOKEN }}
        with:
          github_server: 'github.com'
          source_file: ${{ env.RELEASE_BIN }}.json
          destination_repo: 'aegoroff/scoop-bucket'
          user_email: 'egoroff@email.com'
          user_name: 'aegoroff'
          commit_message: "release: ${{ steps.get_version.outputs.VERSION }}"

      - name: Release Windows tarball
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./windows/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.WINDOWS_TARGET }}.tar.gz
          asset_content_type: application/gzip
          asset_name: ${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.WINDOWS_TARGET }}.tar.gz

      - name: Release Windows installer
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./windows/${{ env.RELEASE_BIN }}.setup.${{ steps.get_version.outputs.VERSION }}.${{ env.Revision }}.exe
          asset_content_type: application/exe
          asset_name: ${{ env.RELEASE_BIN }}.setup.${{ steps.get_version.outputs.VERSION }}.${{ env.Revision }}.exe